package edu.university.demo.system.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.smallchill.core.toolbox.Func;

import edu.university.demo.common.AjaxResult;
import edu.university.demo.core.annotation.Before;
import edu.university.demo.core.annotation.Permission;
import edu.university.demo.system.entit.User;
import edu.university.demo.system.intercept.RegisterValidator;
import edu.university.demo.system.service.AccountService;

/**
 * 管理员管理用户的Controller.
 * 
 * @author mayanping
 */
@Controller
@RequestMapping(value = "/system/user")
public class UserAdminController extends BaseController{

	@Autowired
	private AccountService accountService;

	/*@Permission({"admin"})*/
	@RequestMapping(value="/list", method = RequestMethod.GET)
	public String list(Model model) {
		List<User> users = accountService.getAllUser();
		model.addAttribute("userList", users);

		return "admin/user/userList";
	}

	@Permission({"admin"})
	@RequestMapping(value = "update/{id}", method = RequestMethod.GET)
	public String updateForm(@PathVariable("id") Long id, Model model) {
		model.addAttribute("user", accountService.getUser(id));
		return "admin/user/addUser";
	}
	
	@Permission({"admin"})
	@RequestMapping(value="/add", method=RequestMethod.GET)
	public String addUserInput() {
		
		return "admin/user/addUser";
	}
	
	@Permission({"admin"})
	@ResponseBody
	@RequestMapping(value="/add", method=RequestMethod.POST)
	public AjaxResult addUser(User user) {
		
		try {
			if(Func.isEmpty(user.getId()))
				accountService.registerUser(user);
			else
				accountService.updateUser(user);
			return success();
		} catch (Exception e) {
			e.printStackTrace();
			return error("系统出错");
		}
	}

	@Permission({"admin"})
	@ResponseBody
	@RequestMapping(value = "update", method = RequestMethod.POST)
	public AjaxResult update(User user) {
		try {
			accountService.updateUser(user);
			return success();
		} catch (Exception e) {
			e.printStackTrace();
			return error();
		}
	}

	
	/*@Permission({"admin"})*/
	@ResponseBody
	@RequestMapping(value = "delete/{id}")
	public AjaxResult delete(@PathVariable("id") Long id) {
		try {
			accountService.deleteUser(id);
			return success();
		} catch (Exception e) {
			e.printStackTrace();
			return error();
		}
	}

	/**
	 * 所有RequestMapping方法调用前的Model准备方法, 实现Struts2 Preparable二次部分绑定的效果,先根据form的id从数据库查出User对象,再把Form提交的内容绑定到该对象上。
	 * 因为仅update()方法的form中有id属性，因此仅在update时实际执行.
	 */
	@ModelAttribute
	public void getUser(@RequestParam(value = "id", defaultValue = "-1") Long id, Model model) {
		if (id != -1) {
			model.addAttribute("user", accountService.getUser(id));
		}
	}
}
